package com.neuedu.his.controller;

import com.neuedu.his.po.Checkapply;
import com.neuedu.his.po.Department;
import com.neuedu.his.po.Fmeditem;
import com.neuedu.his.service.CheckapplyService;
import com.neuedu.his.service.DepartmentService;
import com.neuedu.his.service.FmeditemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/checkApply")
public class CheckApplyController {

    @Autowired
    private FmeditemService fmeditemService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CheckapplyService checkapplyService;
    @GetMapping("/getFmeditems")
    public List<Fmeditem> getFmeditems(@RequestParam String recordTypes,
                                       @RequestParam(required = false) String keyword) {
        // 转换recordTypes参数
        List<Integer> typeList = Collections.emptyList();
        if (recordTypes != null && !recordTypes.isEmpty()) {
            typeList = Arrays.stream(recordTypes.split(","))
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
        }

        // 处理keyword参数
        String searchKeyword = (keyword != null && !keyword.trim().isEmpty()) ? keyword.trim() : null;

        return fmeditemService.getFmeditemsByCondition(typeList, searchKeyword);
    }

    @GetMapping("/getDepartments")
    public List<Department> getDepartments(@RequestParam(required = false) String deptIds) {
        // 如果参数为空或空字符串，返回空列表
        if (deptIds == null || deptIds.trim().isEmpty()) {
            return Collections.emptyList();
        }

        try {
            // 将逗号分隔的字符串转换为List<Integer>
            String[] idArray = deptIds.split(",");
            List<Integer> idList = Arrays.stream(idArray)
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());

            if (idList.isEmpty()) {
                return Collections.emptyList();
            }

            return departmentService.getDepartmentsByIds(idList);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("科室ID格式不正确");
        }
    }

    @PostMapping("/createCheckApply")
    public boolean createCheckApply(@RequestParam Integer medicalRecordId,
                                    @RequestParam Integer registerId,
                                    @RequestParam Integer doctorId,
                                    @RequestParam List<Integer> itemIds,
                                    @RequestParam List<String> objectives,
                                    @RequestParam List<String> positions,
                                    @RequestParam List<Integer> isUrgents,
                                    @RequestParam List<Integer> nums) {
        // 基本参数非空检查
        if (medicalRecordId == null || registerId == null || doctorId == null) {
            throw new IllegalArgumentException("病历ID、挂号ID和医生ID不能为空");
        }

        // 列表参数非空检查
        if (itemIds == null || objectives == null || positions == null ||
                isUrgents == null || nums == null) {
            throw new IllegalArgumentException("所有列表参数不能为null");
        }

        // 列表长度一致性检查
        if (itemIds.isEmpty() ||
                itemIds.size() != objectives.size() ||
                itemIds.size() != positions.size() ||
                itemIds.size() != isUrgents.size() ||
                itemIds.size() != nums.size()) {
            throw new IllegalArgumentException("所有列表参数长度必须一致且不为空");
        }

        // 检查项目ID有效性
        for (Integer itemId : itemIds) {
            if (itemId == null || itemId <= 0) {
                throw new IllegalArgumentException("检查项目ID无效");
            }
        }

        return checkapplyService.createCheckApply(medicalRecordId, registerId, doctorId,
                itemIds, objectives, positions, isUrgents, nums);
    }

    @GetMapping("/getPendingChecks")
    public List<Checkapply> getPendingChecks(@RequestParam(required = false) Integer deptId) {
        // 如果传入了科室ID，则查询该科室所有检查项目
        if (deptId != null) {
            return checkapplyService.getChecksByDeptWithPatientName(deptId);
        }
        // 否则查询所有检查项目
        return checkapplyService.getAllChecksWithPatientName();
    }

    @PostMapping("/updateToPaid")
    public boolean updateToPaid(@RequestParam List<Integer> checkApplyIds) {
        if (checkApplyIds == null || checkApplyIds.isEmpty()) {
            throw new IllegalArgumentException("检查申请ID列表不能为空");
        }
        return checkapplyService.updateState(checkApplyIds, 2); // 2表示已支付
    }

    @PostMapping("/startCheck")
    public boolean startCheck(@RequestParam Integer checkApplyId,
                              @RequestParam Integer operatorId) {
        if (checkApplyId == null || operatorId == null) {
            throw new IllegalArgumentException("检查申请ID和操作员ID不能为空");
        }
        return checkapplyService.startCheck(checkApplyId, operatorId);
    }


}